<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/11/30
 * Time: 14:52
 */

namespace app\api\controller;


use language\Languagemsg;
use think\Db;

class Friend extends Base
{
    private $user_id; // 用户ID
    
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        $this->msg_language = new Languagemsg();
        $this->language = input('language') ? input('language') : 0;
        $this->user_id = input('user_id');
        $time = input('time');
        $value = input('value');
        $reqKey = input('reqKey');
        $auth = $this->checkAll($this->user_id, $time, $value, $reqKey);
        if($auth == 112){
            echo json_encode(['status'=>301, 'msg'=>$this->msg_language->otherLogin[$this->language]]);
            exit;
        }elseif($auth == 2111){
            echo json_encode(['status' => 303, 'msg' => $this->msg_language->frost[$this->language]]);
            exit;
        }
    }
    
    /**
     * 插入好友表
     * @return \think\response\Json
     * other_user_ids       聊过天的好友ID,逗号隔开
     */
    public function insertFriend(){
        $other_user_ids = input('other_user_ids');
        $user_arr = explode(',', trim($other_user_ids, ','));
        if(!empty($user_arr)){
            Db::startTrans();
            try{
                foreach($user_arr as $k => $v){
                    $exist = Db::table('up_friend')
                        ->where(function ($query) use ($v) {
                            $query->where('f_user_id', $this->user_id)
                                ->where('f_other_user_id', $v);
                        })
                        ->whereOr(function ($query1) use ($v){
                            $query1->where('f_other_user_id', $this->user_id)
                                ->where('f_user_id', $v);
                        })
                        ->find();
                    if(!$exist){
                        Db::table('up_friend')->insert(['f_user_id' => $this->user_id, 'f_other_user_id' => $v]);
                    }
                }
                Db::commit();
                return json(['status' => 200, 'msg' => true]);
            }catch(\Exception $e){
                Db::rollback();
                return json(['status' => 0, 'msg' => false]);
            }
        }else{
            return json(['status' => 200, 'msg' => true]);
        }
    }
    
    /**
     * 是否可以聊天
     * @return \think\response\Json
     * other_user_ids      用户ID
     */
    public function isFriend(){
        $other_user_ids = input('other_user_ids');
        $user_arr = explode(',', trim($other_user_ids, ','));
        if(!empty($user_arr)){
            $users = Db::table('up_u_user')
                ->where('user_id', 'IN', $user_arr)
                ->field('user_id, user_username, user_image')
                ->select();
            foreach($users as $k => $v){
                // 是否聊过天
                $is_chat = Db::table('up_friend')
                    ->where(function ($query) use ($v) {
                        $query->where('f_user_id', $this->user_id)
                            ->where('f_other_user_id', $v['user_id']);
                    })
                    ->whereOr(function ($query1) use ($v){
                        $query1->where('f_other_user_id', $this->user_id)
                            ->where('f_user_id', $v['user_id']);
                    })
                    ->find();
                // 是否已经关注
                $is_attent = Db::table('up_my_attent')
                    ->where('att_my_user_id', $this->user_id)
                    ->where('att_other_user_id', $v['user_id'])
                    ->find();
                // 有没有买过该画师的商品
                $is_buy = Db::table('up_goods_order')
                    ->where('order_type', 'NEQ', 5)
                    ->where('order_buy_user_id', $this->user_id)
                    ->where('order_sail_user_id', $v['user_id'])
                    ->where('order_status', 'IN', '4,9')
                    ->where('order_aftersale_status', 2)
                    ->find();
                if($is_attent || $is_chat || $is_buy){
                    $users[$k]['is_friend'] = 1; // 可以聊天
                }else{
                    $users[$k]['is_friend'] = 2; // 不可以聊天
                }
            }
            return json(['status' => 200, 'msg' => $users]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
}